package com.tuanzi.loan.web.bean.system;

import static com.tuanzi.loan.business.constants.DictionaryCode.*;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.system.Schedule;
import com.tuanzi.loan.business.service.system.ScheduleService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.web.configuration.JSFInfoBean;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @author Ray 2017/09/21
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class ScheduleDefinitionInfoBean extends JSFInfoBean<Schedule> {

	private static final long serialVersionUID = -2152778554108785342L;
	@Autowired
	private ScheduleService scheduleService;

	@PostConstruct
	private void init() {
		String id = getParamFromRequest("id");
		if (StringUtils.isNotBlank(id)) {
			$ = scheduleService.findOne(id);
		} else {
			$ = new Schedule();
			changeStatusToAdd();
		}
	}

	public void onSave() {
		$ = scheduleService.saveOrUpdate($);
		changeStatusToView();
		addSuccessfullyMessage();
	}

	@Override
	protected List<String> getDictionaryCodes() {
		return Lists.newArrayList(SCHEDULE_START_TYPE, SCHEDULE_EXECUTE_TYPE, SCHEDULE_TASK_GROUP);
	}
}
